package com.ruoyi.flowable.config;

import org.flowable.engine.HistoryService;
import org.flowable.engine.IdentityService;
import org.flowable.engine.ManagementService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.impl.db.DbIdGenerator;
import org.flowable.spring.ProcessEngineFactoryBean;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
/**
 * 扩展流程配置
 * @author Tony
 * @date 2022-12-26 10:24
 */
@Configuration
public class FlowableConfig implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {
    @Override
    public void configure(SpringProcessEngineConfiguration engineConfiguration) {
        engineConfiguration.setActivityFontName("宋体");
        engineConfiguration.setLabelFontName("宋体");
        engineConfiguration.setAnnotationFontName("宋体");
        engineConfiguration.setIdGenerator(new DbIdGenerator());
    }

    @Bean
    public SpringProcessEngineConfiguration processEngineConfiguration(
            DataSource dataSource, 
            PlatformTransactionManager transactionManager) {
        SpringProcessEngineConfiguration config = new SpringProcessEngineConfiguration();
        config.setDataSource(dataSource);
        config.setTransactionManager(transactionManager);
        config.setDatabaseSchemaUpdate("true"); // 自动更新表结构
        return config;
    }

    @Bean
    public ProcessEngineFactoryBean processEngine(
            SpringProcessEngineConfiguration config) {
        ProcessEngineFactoryBean factory = new ProcessEngineFactoryBean();
        factory.setProcessEngineConfiguration(config);
        return factory;
    }

    // 显式声明 RepositoryService Bean
    @Bean
    public RepositoryService repositoryService(ProcessEngine processEngine) {
        return processEngine.getRepositoryService();
    }

    // 显式声明 RuntimeService Bean
    @Bean
    public RuntimeService runtimeServiceService(ProcessEngine processEngine) {
        return processEngine.getRuntimeService();
    }

    // 显式声明 IdentityService Bean
    @Bean
    public IdentityService identityServiceService(ProcessEngine processEngine) {
        return processEngine.getIdentityService();
    }

    // 显式声明 TaskService Bean
    @Bean
    public TaskService TaskServiceService(ProcessEngine processEngine) {
        return processEngine.getTaskService();
    }

    // 显式声明 HistoryService Bean
    @Bean
    public HistoryService historyServiceService(ProcessEngine processEngine) {
        return processEngine.getHistoryService();
    }
    
    // 显式声明 ManagementService Bean
    @Bean
    public ManagementService managementServiceService(ProcessEngine processEngine) {
        return processEngine.getManagementService();
    }
    
    // // 显式声明 ManagementService Bean
    // @Bean
    // public ProcessEngine managementServiceService(ProcessEngine processEngine) {
    //     return processEngine.getManagementService();
    // }

}
